/**
 * 初始化贷款产品详情对话框  /^\+?[1-9]\d*$/
 */
var MyInfoDlg = {
    MyInfoData: {},
    validateFields: {}
}
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
MyInfoDlg.set = function (key, val) {
    this.MyInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}
/**
 * 关闭此对话框
 */
MyInfoDlg.close = function () {
    window.history.back();
}
/**
 * 验证数据是否为空
 */
MyInfoDlg.validate = function () {
    $('#addForm').data("bootstrapValidator").resetForm();
    $('#addForm').bootstrapValidator('validate');
    return $("#addForm").data('bootstrapValidator').isValid();
};
/**
 * 收集数据
 */
MyInfoDlg.collectData = function () {
    this.set('id').set('name').set("sex").set("dept").set("email").set("phone");
}
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
MyInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 清除数据
 */
MyInfoDlg.clearData = function () {
    this.MyInfoData = {};
}

//初始化数据
MyInfoDlg.initData = function () {
    //状态单选
    var seletsex = $('#isex').val();
    if ($('#id').val() != "") {
        $("#sex option[value=" + seletsex + "]").attr("selected", "selected");
    }
}
MyInfoDlg.isPositiveInteger = function (s) {//是否为正整数
    var re = /^[0-9]+$/;
    return re.test(s)
}
/**
 * 提交修改
 */
MyInfoDlg.editSubmit = function () {
    var name = $('#name').val();
    var sex = $('#sex').val();
    var dept = $('#dept').val();
    var email = $('#email').val();
    var phone = $('#phone').val();
    if (name == "" || name == null) {
        alert("请输入用户名！");
    } else {
        this.clearData();
        this.collectData();
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/my/update", function (data) {
            if (data == 0) {
                Feng.error("用户名已存在！请修改");
            } else {
                alert("修改成功!");
                MyInfoDlg.close();
            }
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.MyInfoData);
        ajax.start();
    }
}
$('#inTest').click(function () {
    var orPassWord = $('#orPassWord').val();
    var passWord = $('#passWord').val();
    var newPassWord = $('#newPassWord').val();
    var reNewPassWord = $('#reNewPassWord').val();

    if (newPassWord != reNewPassWord) {
        alert("新密码输入不一致！");
    } else if (newPassWord == null || newPassWord == "") {
        alert("新密码不为空！");
    } else if (newPassWord.length > 20) {
        alert("新密码長度不大于20！");
    } else {
        $.ajax({
            url: Feng.ctxPath + "/my/updatePassWord",
            type: "post",
            data: {
                "passWord": passWord,
                "newPassWord": newPassWord
            },
            dataType: "json",
            success: function (data) {
                if (data == '0') {
                    alert("密码修改成功！");
                    window.location.href = Feng.ctxPath + "/logout";
                } else {
                    alert("原密码错误，请重新输入原密码！");
                }
            },
            error: function (data) {
                alert('内部故障!');
            }
        });
    }
})
/**
 * 显示部门选择的树
 *
 * @returns
 */
MyInfoDlg.showDeptSelectTree = function () {

    var cityObj = $("#citySel");
    var cityOffset = $("#citySel").offset();
    $("#menuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};
/**
 * 点击部门input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
MyInfoDlg.onClickDept = function (e, treeId, treeNode) {
    $("#citySel").attr("value", instance.getSelectedVal());
    $("#deptid").attr("value", treeNode.id);
};
$(function () {
    var ztree = new $ZTree("treeDemo", "/dept/tree");
    ztree.bindOnClick(MyInfoDlg.onClickDept);
    ztree.init();
    instance = ztree;

    MyInfoDlg.initData();
});
